//
//  CLPushViewController.m
//  chanlin
//
//  Created by Ryan on 14-12-21.
//  Copyright (c) 2014年 chanlin. All rights reserved.
//

#import "CLPushViewController.h"
#import "IFCoreDataManager.h"
#import "CLIndexModel.h"
#import "IFCoreDataManager.h"
#import "NSObject+sagittarius.h"
#import "CLList.h"
#import "CLList+Oper.h"
#import "IFIndexCell.h"
#import "CLGloble.h"
#import "IfengOrderStore.h"
#import "CLSlidersViewController.h"
#import "CLPushAnimatedRight.h"
#import "CLDocViewController.h"
#import "IFIndexFocusCell.h"
#import "IFIndexIconCell.h"
#import "IFIndexSlidesCell.h"
#import "CLComentDataModel.h"
#import "CLTopicViewController.h"
#import "CLVideoViewController.h"
#import "BlockUI.h"

@interface CLPushViewController ()

@property (nonatomic,unsafe_unretained) int pageNum;
@property (nonatomic,strong)UIButton *backButton;

@end

@implementation CLPushViewController

- (instancetype)init{
    self=[super init];
    if(self){
        self.edgesForExtendedLayout = UIRectEdgeNone;
        self.extendedLayoutIncludesOpaqueBars = NO;
        self.modalPresentationCapturesStatusBarAppearance = NO;
        
    }
    return self;
}

- (void)viewDidLoad {
    [super viewDidLoad];
    
    self.backButton=[UIButton buttonWithType:UIButtonTypeCustom];
    [self.backButton setImage:[UIImage imageNamed:@"tb_back.png"] forState:UIControlStateNormal];
    self.backButton.contentEdgeInsets=UIEdgeInsetsMake(7, 7, 7, 7);

    self.backButton.frame=CGRectMake(15.f, 20.f, 44.f, 44.f);
    __weak typeof(self) me=self;
    [self.backButton handleControlEvent:UIControlEventTouchUpInside withBlock:^(id sender) {
        [me.dispatchObj pop];
    }];
    [self.view addSubview:self.backButton];
    
    
    UILabel *title=[[UILabel alloc] init];
    title.font=[UIFont systemFontOfSize:20];
    title.frame=CGRectMake((GlobleWidth-100.f)/2.f, 30.f, 100.f, title.font.lineHeight);
    title.textColor=RGB(98, 101, 113);
    title.textAlignment=NSTextAlignmentCenter;
    title.text=@"推送历史";
    
    [self.view addSubview:title];
    
    CALayer *line=[CALayer layer];
    line.backgroundColor=title.textColor.CGColor;
    line.frame=CGRectMake(0, 64.f, GlobleWidth, 0.5);
    [self.view.layer addSublayer:line];


    
    self.tableView.frame=CGRectMake(0, 64.f, GlobleWidth, GlobleHeight-64.f);
    
    self.view.backgroundColor=[UIColor whiteColor];
    NSSortDescriptor *sort = [NSSortDescriptor sortDescriptorWithKey:@"sort" ascending:YES];
    NSFetchRequest *req = [[NSFetchRequest alloc] initWithEntityName:@"CLList"];
    req.predicate = [NSPredicate predicateWithFormat:@"category=%@", @"push"];
    req.sortDescriptors = @[sort];
    self.fetch = [[NSFetchedResultsController alloc] initWithFetchRequest:req
                                                     managedObjectContext:[IFCoreDataManager sharedInstance].mainMoc
                                                       sectionNameKeyPath:nil
                                                                cacheName:nil];
    self.fetch.delegate=self;
    [self.fetch performFetch:nil];
    
    
    
    if([self isViewLoaded])
        [self reloadTable];
    [self.tableView becameHeadReflashLoading];

    
}

-(void)jTableViewStartHeadLoading:(JTableView *)tableView{
    self.pageNum=1;
    __weak typeof(self) me=self;
    [[CLIndexModel sharedInstance] getListWithCategroy:@{@"channelId":@"push"}
                                              withPage:self.pageNum
                                              lastList:nil
                                               uiBlock:^(BOOL success, id responseObj, NSString *message) {
                                                   if(success){
                                                       [me.tableView stopHeadLoading];
                                                   }else{
                                                       [[CLGloble sharedInstance] showTieAlertMessage:@"加载失败"
                                                                                           subMessage:message];
                                                   }
                                               }];
}

- (void)jTableViewStartBottomMoreLoading:(JTableView *)tableView{
    __weak typeof(self) me=self;
    [[CLIndexModel sharedInstance] getListWithCategroy:@{@"channelId":@"push"}
                                              withPage:self.pageNum+1
                                              lastList:self.fetch.fetchedObjects.lastObject
                                               uiBlock:^(BOOL success, id responseObj, NSString *message) {
                                                   if(success){
                                                       me.pageNum++;
                                                       [me.tableView stopBottomMoreWithScuessLoading];
                                                   }else{
                                                       [me.tableView stopBottomMoreWithFailedLoading];
                                                   }
                                               }];
}

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{
    CLList *newsList=[self.fetch objectAtIndexPath:indexPath];
    return newsList.cHeight;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
    CLList *list=[self.fetch objectAtIndexPath:indexPath];
    IFIndexCell *cell=[self cellForStyle:list.style tableView:tableView];
    [cell loadData:list];
    return cell;
}

- (IFIndexCell *)cellForStyle:(int)style tableView:(UITableView *)tableView{
    if(style==CLList_style_focus){
        static NSString *cellID=@"IFIndexFocusCell";
        IFIndexFocusCell *cell=[tableView dequeueReusableCellWithIdentifier:cellID];
        if(!cell){
            cell=[[IFIndexFocusCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:cellID];
            __weak typeof(self) me=self;
            cell._blkTapResponse=^(CLList *indexList,int index,NSIndexPath *indexPath){
                CLList *list=[indexList.focus sgrGetType:[CLList class] forIndex:index];
                [me clickOnList:list];
            };
        }
        return cell;
    }else if (style==CLList_style_doc_img){
        static NSString *cellID=@"IFIndexIconCell";
        IFIndexIconCell *cell=[tableView dequeueReusableCellWithIdentifier:cellID];
        if(!cell){
            cell=[[IFIndexIconCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:cellID];
        }
        return cell;
    }else if(style==CLList_style_doc){
        static NSString *cellID=@"IFIndexTitleCell";
        IFIndexTitleCell *cell=[tableView dequeueReusableCellWithIdentifier:cellID];
        if(!cell){
            cell=[[IFIndexTitleCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:cellID];
        }
        return cell;
    }else if(style==CLList_style_slide){
        static NSString *cellID=@"IFIndexSlidesCell";
        IFIndexSlidesCell *cell=[tableView dequeueReusableCellWithIdentifier:cellID];
        if(!cell){
            cell=[[IFIndexSlidesCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:cellID];
        }
        return cell;
    }
    
    return [[IFIndexCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:@"IFIndexCell"];
}

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{
    CLList *list=[self.fetch objectAtIndexPath:indexPath];
    [self clickOnList:list];
}

- (void)clickOnList:(CLList *)list{
    if(list.link==CLList_sliders){
        CLSlidersViewController *slider=[CLSlidersViewController new];
        slider.itemId=list.itemId;
        [[CLPushAnimatedRight sharedInstance] pushController:slider];
        
    }else if(list.link==CLList_doc){
        CLDocViewController *doc=[CLDocViewController new];
        doc.itemId=list.itemId;
        [[CLPushAnimatedRight sharedInstance] pushController:doc];
    }else if(list.link==CLList_topic){
        CLTopicViewController *topic=[CLTopicViewController new];
        topic.itemId=list.itemId;
        [[CLPushAnimatedRight sharedInstance] pushController:topic];
    }else if(list.link==CLList_video){
        CLVideoViewController *video=[CLVideoViewController new];
        video.itemId=list.itemId;
        [[CLPushAnimatedRight sharedInstance] pushController:video];
    }else if(list.link==CLList_audio){
        CLDocViewController *doc=[CLDocViewController new];
        doc.isAudio=YES;
        doc.itemId=list.itemId;
        [[CLPushAnimatedRight sharedInstance] pushController:doc];
    }
    
}



- (BOOL)showPopAction{
    return YES;
}


@end
